<%--
  Created by IntelliJ IDEA.
  User: Tsuki
  Date: 2023-07-15
  Time: 11:00
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>商品品类管理</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <script src="${pageContext.request.contextPath}/static/js/jquery.min.js"></script>
    <link rel="stylesheet" href="${pageContext.request.contextPath}/static/layui/css/layui.css" media="all">
    <script src="${pageContext.request.contextPath}/static/layui/layui.js" charset="utf-8"></script>
    <script src="${pageContext.request.contextPath}/static/js/showBigImage.js" charset="utf-8"></script>
    <link rel="stylesheet" href="${pageContext.request.contextPath}/static/css/public.css" media="all">
    <style>
        .layui-table-cell {
            text-align: center;
            height: auto !important;
            white-space: normal;
        }
    </style>
</head>
<body>
<div class="layuimini-container ">
    <div class="layuimini-main">
        <%-- 品类图片列--%>
        <script type="text/html" id="goodsPic">
            <img src="${IMG_SERVER}/goods/{{d.pic}}" onclick="showBigImage(this)" style="width: 100% ; height: 100%"
                 class="goodsPic">
        </script>

        <%-- 品类列表--%>
        <table class="layui-hide" id="goodsCategoryTable" lay-filter="goodsCategoryTableFilter"></table>

        <%-- 行内操作工具条--%>
        <script type="text/html" id="categoryTableBar">
            <a class="layui-btn layui-btn-danger layui-btn-sm data-count-edit" lay-event="update">修改</a>
            <a class="layui-btn layui-btn-danger layui-btn-sm data-count-edit" lay-event="delete">删除</a>
        </script>

        <%-- 上架状态开关按钮--%>
        <script type="text/html" id="categoryStateSwitch">
            <input type="checkbox" name="status" value="{{= d.id }}" title="已上架|已下架" lay-skin="switch"
                   lay-filter="Switch"
                   {{# if (d.state== 1) { }}
                   checked
                   {{# } }}>
        </script>

        <%-- 表头工具条--%>
        <script type="text/html" id="toolbar">
            <div class="layui-btn-container">
                <a class="layui-btn layui-btn-normal layui-btn-sm data-count-edit" lay-event="addCategory">添加品类</a>
                <a class="layui-btn layui-btn-normal layui-btn-sm data-count-edit" lay-event="addCategoryItems">添加品项</a>
            </div>
        </script>
    </div>
</div>
<script>
    layui.use(['table', 'treeTable'], function (message) {
        var treeTable = layui.treeTable,
            table = layui.table;
        var addtypes;

        /**
         * 品类树表渲染
         */
        var inst = treeTable.render({
            elem: '#goodsCategoryTable',
            url: '${pageContext.request.contextPath}/manager/category/categoryParentList',
            maxHeight: 600,
            toolbar: '#toolbar',
            tree: {
                customName: {pid: 'pid'},
                data: {isSimpleData: true},
                view: {},
            },

            cols: [[
                {field: 'id', title: '品类编号', width: 135, sort: true},
                {field: 'name', title: '品类名称', width: 180,},
                {field: 'state', width: 135, title: '上架状态', align: 'center', templet: "#categoryStateSwitch"},
                {field: 'priority', title: '显示优先级', width: 150, sort: true},
                {field: 'info', title: '品类信息', width: 190, sort: true},
                {
                    field: 'createTime', title: '创建时间', width: 140, sort: true, templet:
                        function (d) {
                            var timeStr = d.createTime.toString();
                            var timeArr = timeStr.split(",");
                            var year = timeArr[0];
                            var month = timeArr[1];
                            var day = timeArr[2];
                            var hour = timeArr[3];
                            var minute = timeArr[4];
                            var second = timeArr[5];
                            return year + '年' + month + '月' + day + '日 ' + hour + ':' + minute + ':' + second;
                        }
                },
                {
                    field: 'updateTime', title: '更新时间', width: 140, sort: true, templet:
                        function (d) {
                            var timeStr = d.updateTime.toString();
                            var timeArr = timeStr.split(",");
                            var year = timeArr[0];
                            var month = timeArr[1];
                            var day = timeArr[2];
                            var hour = timeArr[3];
                            var minute = timeArr[4];
                            var second = timeArr[5];
                            return year + '年' + month + '月' + day + '日 ' + hour + ':' + minute + ':' + second;
                        }
                },
                {title: "操作", width: 200, align: "center", toolbar: "#categoryTableBar"}
            ]],

        });

        /**
         * 表头工具条事件
         */
        table.on('toolbar(goodsCategoryTableFilter)', function (obj) {
            if (obj.event === 'addCategory') {//做ajax请求时附带类型参数   如果是添加品类就不展示父级类别选项
                addtypes = 0;
                var index = layer.open({
                    title: '添加父级品类',
                    type: 2,
                    shade: 0.2,
                    offset: 'lt',
                    maxmin: true,
                    shadeClose: true,
                    area: ['100%', '100%'],
                    content: '${pageContext.request.contextPath}/manager/category/openAddUpdate?addtypes=' + addtypes,
                    end: function () {
                        treeTable.reload('goodsCategoryTable', {}, 'data');
                    }
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
            } else if (obj.event === 'addCategoryItems') {
                var index = layer.open({
                    title: '添加子类品项',
                    type: 2,
                    shade: 0.2,
                    offset: 'lt',
                    maxmin: true,
                    shadeClose: true,
                    area: ['100%', '100%'],
                    content: '${pageContext.request.contextPath}/manager/category/openAddUpdate',
                    end: function () {
                        treeTable.reload('goodsCategoryTable', {}, 'data');
                    }
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
            }
        });

        /**
         * 行内操作按钮事件
         */
        table.on('tool(goodsCategoryTableFilter)', function (obj) {
            var data = obj.data;
            /**
             * 修改品类信息
             */
            if (obj.event === 'update') {
                var viewIndex = layer.open({
                    title: '品类信息',
                    type: 2,
                    shade: 0.2,
                    offset: 'lt',
                    maxmin: true,
                    shadeClose: true,
                    area: ['100%', '100%'],
                    content: '${pageContext.request.contextPath}/manager/category/openAddUpdate?id=' + data.id,
                    end: function () {
                        treeTable.reload('goodsCategoryTable', {}, 'data');
                    }
                });
                $(window).on("resize", function () {
                    layer.full(viewIndex);
                });

            }
            /**
             * 更新商品
             */
            if (obj.event === 'delete') {
                alert('做ajax请求删除  弹窗确认')
            }
        });

        /**
         * 上下架状态开关事件
         */
        form.on('switch(Switch)', function (obj) {
            $.ajax({
                url: '${pageContext.request.contextPath}/manager/goods/updateState',
                data: {
                    id: this.value
                },
                type: 'GET',
                success: function () {
                    layer.msg('更新失败')
                },
                error: function (xhr, textStatus, errorThrown) {
                    layer.msg('更新成功')
                }
            });
            layer.tips('温馨提示：请注意开关状态的文字可以随意定义，而不仅仅是 ON|OFF', data.othis)
        });
    });
</script>
</body>
</html>
